/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package WeatherSystemMain;

import CommonValue.SystemTimer;
import CommonValue.TriggerConfig;
import DTO.ControlDTO;
import SocketBusiness.ClientConnect;
import DTO.PacketDTO;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class TriggerToAS extends ClientConnect {

    PacketDTO _trigger;

    public TriggerToAS(String systemSign, String ServerIP, int port, ControlDTO operation, PacketDTO _triggerToAS) {
        super(systemSign, ServerIP, port, operation);
        this._trigger = _triggerToAS;
    }

    @Override
    public void business() {
        boolean finish = false;
        do {
            try {
                Thread.sleep(SystemTimer.CheckTriggerSleep);
            } catch (InterruptedException ex) {
                Logger.getLogger(TriggerToAS.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            if (!this.getOperation().getFlag()) {
                System.out.println(this.getSystemSign() + " DA DUNG HOAT DONG <<<<<<<<<<<<<<<<");
                continue;
            }

            synchronized (_trigger) {
                System.out.println("---------buffer to AC size : " + _trigger.getBuffer().size());
                if (_trigger.getBuffer().size() > TriggerConfig.CountTrigger) // neu co hon 4 dieu kien thoi tiet vuot bien ta moi bao ve trung tam
                {
                    try {
                        this.sendMessage(this._trigger);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(TriggerToAS.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    _trigger.clearBuffer();
                }
            }
        } while (!finish);
    }
}
